// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    cardNum:'',
    passNum:'',
    userInfo:'',
    hasUserInfo:false,
    goods:'',
    openid: ''
  },
  cardNum:function(e){
    this.setData({
      cardNum:e.detail.value
    })
  },
  passNum:function(e){
    this.setData({
      passNum:e.detail.value
    })
  },
  goDetail:function(){
    var that=this;
    wx.request({
      url: app.globalData.api + '&r=exchange.exchange_goods.checkisset',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        serial: this.data.cardNum,
        key: this.data.passNum,
        openid:this.data.openid
      },
      success: function (res) {
        if (res.data.error==1){
          wx.showModal({
           title: '提示',
           content:res.data.error_message,
           showCancel:false,
         })
        } else if (res.data.error == 0){
          that.setData({
            goods: res.data.goods[0]
          })
          wx.showModal({
            title: '提示',
            content: '验证成功',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../detail/detail?goods=' + that.data.goods + '&cardNum=' + that.data.cardNum + '&passNum=' + that.data.passNum,
                })
              } 
            }
          })
        }
      },
      error: function () {
        console.log('请求错误');
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'openId',
      success(res) {
        that.setData({
          openid: res.data
        })
      },
      fail() {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: app.globalData.api +'&r=exchange.exchange_goods.home_banner',
      method: 'POST',
      success:function(res){
        that.setData({
          imgUrls: res.data.banners_info
        })
      },
      error:function(){
        console.log('请求错误');
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})